define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'yunfood/food/category/index' + location.search,
                    add_url: 'yunfood/food/category/add',
                    edit_url: 'yunfood/food/category/edit',
                    del_url: 'yunfood/food/category/del',
                    table: 'yunfood_food_category',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                sortOrder: 'asc',
                columns: [
                    [
                        {
                            field: 'state',
                            checkbox: true
                        },
                        {
                            field: 'id',
                            title: 'ID',
                        },
                        {
                            field: 'shopId',
                            title: __('限定门店'),
                            searchList: Config.shopList,
                            formatter: function (value, row, index) {
                                return row.shop_text;
                            },
                        },
                        {
                            field: 'name',
                            title: __('名称'),
                        },
                        {
                            field: 'weigh',
                            title: __('排序'),
                            sortable: true,
                            operate: false,
                        },
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: Config.statusList,
                            formatter: Table.api.formatter.label
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({},this);
                                var table = $(this.table).clone(true);
                                if (row.mute_operate) {
                                    $(table).data('operate-edit', null);
                                    $(table).data('operate-del', null);
                                    $(table).data('operate-dragsort', null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ],
                ],
                // 快捷搜索,这里可在控制器定义快捷搜索的字段
                search: false,
                // 启用普通表单搜索
                commonSearch: true,
                // 显示切换视图（table/card）按钮
                showToggle: false,
                // 显示导出按钮
                showExport: true,
                // 启用跨页选择
                maintainSelected: true,
                // 启用固定列
                fixedColumns: true,
                // 固定右侧列数
                fixedRightNumber: 1,
                // 可以控制是否默认显示搜索单表,false则隐藏,默认为false
                searchFormVisible: true,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {  // 渲染的方法
            },
            events: {  // 绑定事件的方法
            }
        }
    };
    return Controller;
});
